# 6.10 Integration with Spring Boot

# 1. Maven Coordinates

For integration with Spring, you can configure the Maven coordinates for JFinal or the standalone version of the Enjoy Template Engine in your pom.xml file. The Maven coordinates for Enjoy are as follows:

<dependency>
  <groupId>com.jfinal</groupId>
  <artifactId>enjoy</artifactId>
  <version>5.1.2</version>
</dependency>
1
2
3
4
5

The standalone version of the JFinal Template Engine, Enjoy, has a size of only 207KB and has no third-party dependencies.

# 2. Spring Boot Integration

The configuration for integrating with Spring Boot is as follows:

@Configuration
public class SpringBootConfig {

  @Bean(name = "jfinalViewResolver")
  public JFinalViewResolver getJFinalViewResolver() {

    // Create a ViewResolver extension object for Spring Boot integration
    JFinalViewResolver jfr = new JFinalViewResolver();
    
    // Configure Spring Boot
    jfr.setSuffix(".html");
    jfr.setContentType("text/html;charset=UTF-8");
    jfr.setOrder(0);
    
    // Enable session data access in the template via #(session.value)
    jfr.setSessionInView(true);
    
    // Get the engine object to configure the Enjoy template engine
    Engine engine  = JFinalViewResolver.engine;
    
    // Enable dev mode for hot reloading, must be set before other configurations
    engine.setDevMode(true);

    // Use ClassPathSourceFactory to load templates from the classpath and jar files
    engine.setToClassPathSourceFactory();
    
    // Use setBaseTemplatePath when using ClassPathSourceFactory
    engine.setBaseTemplatePath("/view/");
    
    // Add shared functions
    engine.addSharedFunction("/common/_layout.html");
    engine.addSharedFunction("/common/_paginate.html");
    
    // More configurations can be done in the same way as previously mentioned
    // engine.addDirective(...)
    // engine.addSharedMethod(...);
    
    return jfr;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

As shown above, jfr.setToClassPathSourceFactory() configures the ClassPathSourceFactory to load template files from the classpath and jar files. jfr.addSharedFunction(...) configures shared template functions. Essentially, the configuration for Enjoy is consistent with the configuration of the Engine object.

If you want to load template files from the project's webapp directory, there is no need to set it to ClassPathSourceFactory.

# 3. Spring MVC Integration

Integrating Enjoy with Spring MVC is quite simple and only requires the configuration of a single bean. The detailed configuration method is as follows:

<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
  <!-- Enable hot reloading of template files -->
  <property name="devMode" value="true"/>
  <!-- Configure shared functions, multiple files separated by commas -->
  <property name="sharedFunction" value="/view/_layout.html, /view/_paginate.html"/>
  
  <!-- Enable session data access in the template via #(session.value) -->
  <property name="sessionInView" value="true"/>
  <property name="prefix" value="/view/"/>
  <property name="suffix" value=".html"/>
  <property name="order" value="1"/>
  <property name="contentType" value="text/html; charset=utf-8"/>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13

For more detailed configurations and their explanations, you can refer to the comments in the header of JFinalViewResolver. In most cases, the above configurations should be sufficient.

Last Updated: 9/22/2023, 4:50:57 AM